package com.livelab.java;

import com.livelab.model.ExerciseProgressSummary;
import com.livelab.util.MySQLDatabaseUtil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

/**
 *Backing bean that supports "exerciseProgressSummary.xhtml"
 * 
 * @author Joey
 */
@ManagedBean
@RequestScoped
public class ExerciseProgressSummaryBean {

    MySQLDatabaseUtil db; //database
    ResultSet temp;  //a resultset temp
    ResultSet singleScore; //a resultset to display singleScores
    double maxTotalScore; //max total score a student can have
    double totalScore; //students total score
    double percentage; //students percent
    int numberStudents = 0; //the total number of students
    int exercisesSubmitted; //total exercises a student submitted
    List<ExerciseProgressSummary> exerciseProgressSummary; //list of all students and info
    String studentUsername; //the student's username
    String instructorUsername; //the instructor's username
    
    @ManagedProperty(value = "#{instructorLoginBean}")
    private InstructorLoginBean instructor; //instructor login bean

    /**
     * The Constructor
     */
    public ExerciseProgressSummaryBean() {
        db = new MySQLDatabaseUtil("localhost/javalivelab9e", "agsuser", "agsuser");
        exerciseProgressSummary = new ArrayList<>();
    }

    @PostConstruct
    public void init() {
        ResultSet rs;
        rs = db.executeQuery("SELECT agsstudent.firstname, agsstudent.lastname, agsstudent.username, agsinstructor.username FROM agsinstructor, agsstudent WHERE agsstudent.courseID = '" + instructor.getCourseId() + "';");
        try {
            //getting all the students that belong to the instructor
            while (rs.next()) {
                if(rs.getString(3).equals(studentUsername)) continue;
                studentUsername = rs.getString(3);
                instructorUsername = rs.getString(4);
                totalScore = 0;
                maxTotalScore = 0;
                percentage = 0;
                exercisesSubmitted = 0;

                temp = db.executeQuery("SELECT score FROM exerciseassigned  WHERE username = '" + instructorUsername + "';");
                while (temp.next()) {
                    maxTotalScore += temp.getDouble(1);
                }
                ResultSet singleScore = db.executeQuery("SELECT score from agslog where username = '" + studentUsername + "';");
                //obtain all the scores of the student
                while (singleScore.next()) {
                    totalScore += singleScore.getDouble(1);
                    //if exercise has a score, it was submitted
                    if(singleScore.getDouble(1) > 0){
                        exercisesSubmitted += 1;
                    }
                }
                
                //calculating the percent and adding all the info the the table
                percentage = (totalScore / maxTotalScore) * 100;
                exerciseProgressSummary.add(new ExerciseProgressSummary(rs.getString(2), rs.getString(1), totalScore, maxTotalScore, percentage, exercisesSubmitted));
                
                temp.close();
                singleScore.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        numberStudents = exerciseProgressSummary.size(); //obtains total number of students
    }

    /*
     * GETTERS
     */
    public int getNumberStudents() {
        return numberStudents;
    }

    public List<ExerciseProgressSummary> getExerciseProgressSummary() {
        return exerciseProgressSummary;
    }

    /*
     * SETTERS
     */
    public void setInstructor(InstructorLoginBean instructor) {
        this.instructor = instructor;
    }
}